package cn.wit.db.web;

import cn.wit.db.entity.AjaxResult;
import cn.wit.db.entity.Employees;
import cn.wit.db.entity.UserRole;
import cn.wit.db.mapper.EmployeesMapper;
import cn.wit.db.mapper.UserRoleMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class EmployeesController {
    private static final Logger logger = LoggerFactory.getLogger(EmployeesController.class);

    @Autowired
    EmployeesMapper mapper;

    @Autowired
    PasswordEncoder encoder;

    @Autowired
    UserRoleMapper userRoleMapper;

    //查询
    @GetMapping("/employees")
    public List<Employees> getEmployees(){
        return mapper.selectAll();
    }

    //插入
    @PostMapping("/employees")
    public AjaxResult createEmployees(Employees employees){
        employees.setPassword(encoder.encode("123456"));

        mapper.insert(employees);

        System.out.println(employees.getEno() + " === ");

        UserRole userRole = new UserRole();
        userRole.setEno(employees.getEno());
        userRole.setId(1);      // default to admin id
        userRoleMapper.insert(userRole);


        return AjaxResult.success();
    }

    //删除
    @DeleteMapping("/employees")
    public AjaxResult deleteEmployees(@RequestParam int eno){
         mapper.deleteByPrimaryKey(eno);
         return AjaxResult.success();
    }

    //修改
    @PutMapping("/employees")
    public AjaxResult updateEmployees(@PathVariable Employees employees){
        mapper.updateByPrimaryKey(employees);
        return AjaxResult.success();
    }

}
